import axios from '@/api/axios';
import { useRouter } from "vue-router";
import { Message } from '@arco-design/web-vue';

export function annoTaskSearch(model: any) {
  return axios.post('/anno/task/search', model);
}

export async function annoResultExport(model: any) {
  const router = useRouter();
  const res = await axios.request({
    url: '/anno/result/export',
    method: 'post',
    data: model,
    responseType: 'blob',
    headers: {
      ContentType: 'form-data',
    },
  });
  if (res.status === 200) {
    if (res.data.type === "application/json") {
      const blob = new Blob([res.data], { type: "text/plain" })
      const fileReader = new FileReader();
      fileReader.readAsText(blob)
      fileReader.onload = (e) => {
        const text = fileReader.result;
        if (text && typeof text === "string") {
          const json = JSON.parse(text);
          json.code === 501 && Message.error(json.msg) && router.push("/login");
          json.code !== 0 && Message.warning(json.msg || '无导出数据');
        }
      };
    }
    if (res.data.type === "application/octet-stream") {
      if (model.taskIds.length === 1) {
        const blob = new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
        const temp = res.headers["content-disposition"].split(";")[2].split("filename=")[1].split('"')[1];
        const fileName = decodeURIComponent(temp);
        const a = document.createElement("a");
        if (!('download' in a)) {
          Message.warning('该浏览器下载不支持')
          return
        }
        const href = window.URL.createObjectURL(blob);
        a.href = href;
        a.download = fileName;
        (document.body || document.documentElement).appendChild(a);
        a.click();
        (document.body || document.documentElement).removeChild(a);
        window.URL.revokeObjectURL(href);
      }
      if (model.taskIds.length > 1) {
        const blob = new Blob([res.data], { type: "application/zip" });
        const temp = res.headers["content-disposition"].split(";")[2].split("filename=")[1].split('"')[1];
        const fileName = decodeURIComponent(temp);
        const a = document.createElement("a");
        if (!('download' in a)) {
          Message.warning('该浏览器下载不支持')
          return
        }
        const href = window.URL.createObjectURL(blob);
        a.href = href;
        a.download = fileName;
        (document.body || document.documentElement).appendChild(a);
        a.click();
        (document.body || document.documentElement).removeChild(a);
        window.URL.revokeObjectURL(href);
      }
    }
  }
  
}

export const taskExport = async (params: { seriesUids: string[] }) => {
  const router = useRouter();
  const res = await axios.request({
    method: "post",
    url: "/series/task/export",
    data: params,
    responseType: 'blob',
    headers: {
      ContentType: 'form-data',
    }
  });
  if (res.status === 200) {
    if (res.data.type === "application/json") {
      const blob = new Blob([res.data], { type: "text/plain" })
      const fileReader = new FileReader();
      fileReader.readAsText(blob)
      fileReader.onload = (e) => {
        const text = fileReader.result;
        if (text && typeof text === "string") {
          const json = JSON.parse(text);
          json.code === 501 && Message.error(json.msg) && router.push("/login");
          json.code !== 0 && Message.error(json.msg);
        }
      };
    }
    if (res.data.type === "application/octet-stream") {
      if (params.seriesUids.length === 1) {
        const blob = new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
        const temp = res.headers["content-disposition"].split(";")[2].split("filename=")[1].split('"')[1];
        const fileName = decodeURIComponent(temp);
        const a = document.createElement("a");
        if (!('download' in a)) {
          Message.warning('该浏览器下载不支持')
          return
        }
        const href = window.URL.createObjectURL(blob);
        a.href = href;
        a.download = fileName;
        (document.body || document.documentElement).appendChild(a);
        a.click();
        (document.body || document.documentElement).removeChild(a);
        window.URL.revokeObjectURL(href);
      }
      if (params.seriesUids.length > 1) {
        const blob = new Blob([res.data], { type: "application/zip" });
        const temp = res.headers["content-disposition"].split(";")[2].split("filename=")[1].split('"')[1];
        const fileName = decodeURIComponent(temp);
        const a = document.createElement("a");
        if (!('download' in a)) {
          Message.warning('该浏览器下载不支持')
          return
        }
        const href = window.URL.createObjectURL(blob);
        a.href = href;
        a.download = fileName;
        (document.body || document.documentElement).appendChild(a);
        a.click();
        (document.body || document.documentElement).removeChild(a);
        window.URL.revokeObjectURL(href);
      }
    }
  }
}

const writeFile = () => {
  // "application/json"(){
    
  // }
}